//
//  MYNetworkTool.swift
//  NewMicroblog
//
//  Created by apple on 2016/10/25.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import AFNetworking

enum requestMethod : String {
    case get
    case post
}

class MYNetworkingTool: AFHTTPSessionManager {
   
    static let shareTool:MYNetworkingTool = {
        let tool = MYNetworkingTool()
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    
    /// 封装的请求工具类
    ///
    /// - parameter requestMethod: 请求方式
    /// - parameter URLString:     路径
    /// - parameter parameters:    参数
    /// - parameter success:       成功
    /// - parameter failure:       失败
    func request(requestMethod:requestMethod,URLString: String, parameters: Any?, success: @escaping (Any?) -> (), failure: @escaping (Error) ->()){
        if requestMethod == .get {
            get(URLString, parameters: parameters, progress: nil, success: { (URLSessionDataTask, response) in
                success(response)
                }, failure: { (URLSessionDataTask, error) in
                    failure(error)
            })
        }else{
            
            post(URLString, parameters: parameters, progress: nil, success: { (URLSessionDataTask, response) in
                success(response)
                }, failure: { (URLSessionDataTask, error) in
                    failure(error)
            
        })
    
        }
    
    }
   
    
}

// MARK: - 发表微博接口
extension MYNetworkingTool{
    func update(token: String, status: String, success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        let url = "https://api.weibo.com/2/statuses/update.json"
        let pramas:[String : Any] = [
            "access_token": token,
            "status": status
        ]
        request(requestMethod: .post, URLString: url, parameters: pramas, success: success, failure: failure)
    }
}

extension MYNetworkingTool{
    func oauthUserAccount(code: String, success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id": MYAPPKEY,
            "client_secret": MYSECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": MYREDIRECTURI
        
        ]
        request(requestMethod: .post, URLString: urlString, parameters: params, success: success, failure: failure)
    }
    func oauthUserInfo(model:MYUserinfoModel,success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 请求参数
        let params = [
            "access_token":model.access_token!,
            "uid":model.uid!
        ]
        
        request(requestMethod: .get, URLString: urlString, parameters: params, success: success, failure: failure)

    }
    
    
}
extension MYNetworkingTool{
    func homeLoadData(sinceId: Int64, maxId: Int64, success:@escaping (Any?)->(),failure:@escaping (Error)->()){
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        let params: [String: Any] = [
            "access_token": MYUserinfoViewModel.sharedModel.accessToken!,
            "since_id":sinceId,
            "max_id":maxId
        ]
        
        request(requestMethod: .get, URLString: urlStr, parameters: params, success: success, failure: failure)
    }
    
    
}
//MARK:- 发送图片的微博接口
extension MYNetworkingTool{
    func uploadImage(token: String, status: String, image: UIImage, success:@escaping (Any?)->(), failure:@escaping (Error)->()){
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        let pramas = [
                        "access_token": token,
                                    "status": status
        ]
        let imageData = UIImageJPEGRepresentation(image, 0.6)!
        post(url, parameters: pramas, constructingBodyWith: { (formData) in
            formData.appendPart(withFileData: imageData, name: "pic", fileName: "test", mimeType: "image/png")
        }, progress: nil, success: { (_, response) in
            success(response)
        }) { (_, error) in
            failure(error)
        }
    }
}




